package com.nondo.dean.auth.controller.api;

import com.nondo.dean.auth.common.bean.MenuItem;
import com.nondo.dean.auth.common.bean.MenuRoleItem;
import com.nondo.dean.auth.common.bean.TreeBean;
import com.nondo.dean.auth.domain.Menu;
import com.nondo.dean.auth.provider.MenuManager;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单对外接口
 *
 * @author linfeng
 */
@RestController
@RequestMapping("/{v}/menu")
public class MenuController {

    @Autowired
    private MenuManager menuManager;

    /**
     * 菜单注册
     *
     * @param menuItem 菜单项
     * @return 返回注册成功或失败的信息
     */
    @ApiVersion(1)
    @RequestMapping(
            value = {"/register"},
            method = RequestMethod.POST
    )
    public View<String> requestMenuRegister(@RequestBody @Validated MenuItem menuItem) {

        return menuManager.requestMenuRegister(menuItem);
    }

    /**
     * 菜单注册
     *
     * @param menuId 菜单项
     * @return 返回注册成功或失败的信息
     */
    @ApiVersion(1)
    @RequestMapping(
            value = {"/delete/{menuId}"},
            method = RequestMethod.POST
    )
    public View<String> requestMenuDelete(@PathVariable Long menuId) {

        return menuManager.requestMenuDelete(menuId);
    }

    /**
     * 菜单注册
     *
     * @param menuId 菜单项
     * @return 返回注册成功或失败的信息
     */
    @ApiVersion(1)
    @RequestMapping(
            value = {"/one/{menuId}"},
            method = RequestMethod.POST
    )
    public View<Menu> requestMenuOne(@PathVariable Long menuId) {

        return menuManager.requestMenuOne(menuId);
    }

    /**
     * 菜单树
     *
     * @return 菜单树
     */
    @ApiVersion(1)
    @RequestMapping(
            value = {"/list"},
            method = RequestMethod.POST
    )
    public View<Page<Menu>> requestMenuQuery(@RequestBody Query<MenuItem> menuItem) {

        return menuManager.requestQuery(menuItem);
    }

    /**
     * 菜单角色绑定
     *
     * @param menuItem 菜单项
     * @return 返回注册成功或失败的信息
     */
    @ApiVersion(1)
    @RequestMapping(
            value = {"/role/bind"},
            method = RequestMethod.POST
    )
    public View<Void> requestRoleBindMenuRegister(@RequestBody @Validated MenuRoleItem menuItem) {
        return menuManager.requestRoleBindMenuRegister(menuItem);
    }

    /**
     * 菜单角色解绑
     *
     * @param menuItem 菜单项
     * @return 返回注册成功或失败的信息
     */
    @ApiVersion(1)
    @RequestMapping(
            value = {"/role/unbind"},
            method = RequestMethod.POST
    )
    public View<Void> requestRoleUnBindMenuRegister(@RequestBody @Validated MenuRoleItem menuItem) {
        return menuManager.requestRoleBindMenuRegister(menuItem);
    }

    /**
     * 菜单树
     *
     * @return 菜单树
     */
    @ApiVersion(1)
    @RequestMapping(
            value = {"/tree/all"},
            method = RequestMethod.POST
    )
    public View<List<TreeBean>> requestMenuTreeOfAll() {

        return menuManager.requestMenuTreeOfAll();
    }

    /**
     * 当前用户有权访问的菜单树
     *
     * @return 菜单树
     */
    @ApiVersion(1)
    @RequestMapping(
            value = {"/tree/user"},
            method = RequestMethod.POST
    )
    public View<List<TreeBean>> requestMenuTreeOfUser() {

        return menuManager.requestMenuTreeOfUser();
    }

}
